package com.qiyuesuo.controller;


import com.qiyuesuo.entity.MyFile;
import com.qiyuesuo.service.FileService;
import com.qiyuesuo.utils.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/api")
public class FileController {

    //注入业务层
    @Autowired
    private FileService fileService;

    //POST请求方式
    @PostMapping("/upload")
    //文件上传接口
    public DataResult<String> upload(@RequestParam(value = "file") MultipartFile file){
        DataResult result= DataResult.success();
        String uuid = fileService.upload(file);
        result.setData(uuid);
        return result;
    }

    //文件下载接口
    @GetMapping("/file/{fileId}")
    public void download(@PathVariable("fileId") String fileId, HttpServletResponse response){
        fileService.download(fileId,response);
    }

    //获取指定文件的元信息
    @PostMapping("/getFileInfo/{fileId}")
    public DataResult<MyFile> getInfo(@PathVariable("fileId") String fileId){
        DataResult result= DataResult.success();
        MyFile myFile = fileService.getFileInfo(fileId);
        result.setData(myFile);
        System.out.println(result);
        return result;
    }
}
